﻿using System.Windows;
using System.Windows.Controls;
using MLSharp.Clustering;

namespace KnowledgeFrontierMiner
{
	/// <summary>
	/// Interaction logic for KfdSettings.xaml
	/// </summary>
	public partial class KfdSettings : Window
	{
		#region Private Fields

		/// <summary>
		/// Temporarily stores the attributes that are defined on the dataset.
		/// </summary>
		private string[] mAvailableAttributes;

		#endregion

		#region Public Properties

		/// <summary>
		/// Stores the acuity selected by the user.
		/// </summary>
		public double? Acuity { get; private set; }

		/// <summary>
		/// The alpha entered by the user.
		/// </summary>
		public double? Alpha { get; private set; }

		/// <summary>
		/// The cutoff entered by the user.
		/// </summary>
		public double? Cutoff { get; private set; }

		/// <summary>
		/// The attributes to filter.
		/// </summary>
		public string[] AttributesToFilter { get; private set; }

		#endregion

		#region Public Constructors

		/// <summary>
		/// Designer generated, don't much with.
		/// </summary>
		public KfdSettings(string[] availableAttributes)
		{
			mAvailableAttributes = availableAttributes;

			InitializeComponent();
		}

		#endregion

		#region Methods

		/// <summary>
		/// Fired whenever the user clicks Ok.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OkButtonClick(object sender, RoutedEventArgs e)
		{
			double value;

			if (double.TryParse(AlphaTextBox.Text, out value))
			{
				Alpha = value;
			}

			if (double.TryParse(CutoffTextBox.Text, out value))
			{
				Cutoff = value;
			}

			if (double.TryParse(AcuityTextBox.Text, out value))
			{
				Acuity = value;
			}

			if (RemovedAttributesListBox.Items.Count > 0)
			{
				string[] filteredAttributes = new string[RemovedAttributesListBox.Items.Count];

				RemovedAttributesListBox.Items.CopyTo(filteredAttributes, 0);

				AttributesToFilter = filteredAttributes;
			}

			DialogResult = true;
		}

		/// <summary>
		/// Fired when the window finishes loading.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void WindowLoaded(object sender, RoutedEventArgs e)
		{
			foreach (string attribute in mAvailableAttributes)
			{
				AvailableAttributesListBox.Items.Add(attribute);
			}

			AlphaSlider.Value = KnowledgeFrontierDiscovery.DefaultAlpha;
			AcuitySlider.Value = Cobweb.DefaultAcuity;
			CutoffSlider.Value = Cobweb.DefaultCutoff;
		}

		/// <summary>
		/// Fired when the "Keep" button is clicked.  If something is selected in 
		/// the list of attributes to filter, it is moved back to the list of 
		/// attributes to keep.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void KeepButtonClicked(object sender, RoutedEventArgs e)
		{
			MoveItemsBetweenBoxes(RemovedAttributesListBox, AvailableAttributesListBox);
		}

		/// <summary>
		/// Fired when the Filter button is clicked. 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void FilterButtonClicked(object sender, RoutedEventArgs e)
		{
			MoveItemsBetweenBoxes(AvailableAttributesListBox, RemovedAttributesListBox);
		}

		/// <summary>
		/// Moves the selected item from the source listbox to the destination box.
		/// </summary>
		/// <param name="source"></param>
		/// <param name="destination"></param>
		private void MoveItemsBetweenBoxes(ListBox source, ListBox destination)
		{
			if (source.SelectedIndex >= 0)
			{
				int index = source.SelectedIndex;
				string value = (string)source.SelectedValue;

				source.Items.RemoveAt(index);

				destination.Items.Add(value);
			}
		}

		#endregion
	}
}
